package com.ljd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ljd.entity.UcenterMember;
import com.ljd.entity.vo.LoginInfoVo;
import com.ljd.entity.vo.LoginVo;
import com.ljd.entity.vo.RegisterVo;
import com.ljd.mapper.UcenterMemberMapper;
import com.ljd.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ljd.util.JwtUtils;
import com.ljd.util.MD5;
import com.why.handler.GuliException;
import io.swagger.annotations.Authorization;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author ljd
 * @since 2021-12-30
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    RedisTemplate<String, String> redisTemplate;

    @Override
    public String login(LoginVo loginVo) {
        String mobile = loginVo.getMobile();
        String password = loginVo.getPassword();

        // 必填项校验
        if(StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)) {
            throw new GuliException(20001,"输入为必填项");
        }
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile", mobile);
        UcenterMember ucenterMember = baseMapper.selectOne(wrapper);
        if(ucenterMember == null){
            throw new GuliException(20001,"手机号不正确");
        }
        //判断密码
        //因为存储到数据库密码肯定加密的
        //把输入的密码进行加密，再和数据库密码进行比较
        //加密方式 MD5
        if(!MD5.encrypt(password).equals(ucenterMember.getPassword())){
            throw new GuliException(20001,"密码错误");
        }
        //判断用户是否禁用
        if(ucenterMember.getIsDisabled()){
            throw new GuliException(20001,"手机号不可用");
        }
        //登录成功
        //生成token字符串，使用jwt工具类
        String jwtToken = JwtUtils.getJwtToken(ucenterMember.getId(), ucenterMember.getNickname());
        return jwtToken;
    }

    @Override
    public Boolean register(RegisterVo registerVo) {
        String code = registerVo.getCode();
        String mobile = registerVo.getMobile();
        String nickname = registerVo.getNickname();
        String password = registerVo.getPassword();
        // 输入数据非空校验
        if(StringUtils.isEmpty(code) || StringUtils.isEmpty(mobile)
        ||StringUtils.isEmpty(nickname) || StringUtils.isEmpty(password)){
            throw new GuliException(20001,"数据不能为空");
        }
        // 验证码和redis里存的相同校验
        String sendCode = redisTemplate.opsForValue().get(mobile);
        if(!code.equals(sendCode)){
            throw new GuliException(20001,"验证码输入错误");
        }

        // 手机号不重复校验
        QueryWrapper<UcenterMember> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile",mobile);
        Integer count = baseMapper.selectCount(queryWrapper);
        if(count >0){
            throw new GuliException(20001,"手机号重复");
        }
        UcenterMember member = new UcenterMember();
        member.setMobile(mobile);
        member.setNickname(nickname);
        member.setPassword(MD5.encrypt(password));
        member.setIsDisabled(false);
        member.setAvatar("http://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83eoj0hHXhgJNOTSOFsS4uZs8x1ConecaVOB8eIl115xmJZcT4oCicvia7wMEufibKtTLqiaJeanU2Lpg3w/132");
        int insert = baseMapper.insert(member);
        if(insert>0){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public LoginInfoVo getLoginInfo(String memberId) {
        UcenterMember member = this.getById(memberId);
        if(member == null){
            throw new GuliException(20001,"获取用户信息失败");
        }
        LoginInfoVo loginInfoVo = new LoginInfoVo();
        loginInfoVo.setId(member.getId());
        loginInfoVo.setAvatar(member.getAvatar());
        loginInfoVo.setNickname(member.getNickname());
        loginInfoVo.setAge(member.getAvatar());
        loginInfoVo.setMobile(member.getMobile());
        loginInfoVo.setSex(member.getSex());

        return loginInfoVo;
    }

    @Override
    public UcenterMember getByOpenId(String openid) {
        QueryWrapper<UcenterMember> w = new QueryWrapper<>();
        w.eq("openid",openid)
                .eq("is_disabled",false);
        return baseMapper.selectOne(w);
    }

    @Override
    public Integer getRegisterCount(String day) {
        return baseMapper.getRegisterCount(day);
    }

}
